//
//  EWUserInfoTableViewCell.m
//  haveSomethingToEat
//
//  Created by 陈宁权 on 2017/4/8.
//  Copyright © 2017年 陈 宁权. All rights reserved.
//

#import "EWUserInfoTableViewCell.h"

@interface EWUserInfoTableViewCell()
@property (nonatomic,strong) UIImageView *userLogo;
@property (nonatomic,strong) UILabel *userName;
@property (nonatomic,strong) UILabel *phoneNumber;
@end

@implementation EWUserInfoTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"EWUserInfoTableViewCell";
        // 1.缓存中取
        EWUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        // 2.创建
        if (cell == nil) {
                 cell = [[EWUserInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupChildViews];
    }
    return self;
}

- (void)setupChildViews
{
    //设置背景
    self.backgroundColor = [UIColor colorWithRed:133/255.0 green:32/255.0 blue:133/255.0 alpha:1];
    _userLogo = [[UIImageView alloc] init];
    
    [_userLogo sd_setImageWithURL:[NSURL URLWithString:[RCIM sharedRCIM].currentUserInfo.portraitUri] placeholderImage:[UIImage imageNamed:@"publicNumber"]];
    
    _userLogo.layer.cornerRadius = 25.0 * KWidth_Scale;
    _userLogo.layer.masksToBounds = YES;
    [self.contentView addSubview:_userLogo];
    
    _userName = [[UILabel alloc] init];
    NSString *userNameStr = [RCIM sharedRCIM].currentUserInfo.name;
    if (!(userNameStr.length > 0)) {
        //如果没有用户名，生成11位随机用户名
        userNameStr = [self createRandomStrwithDigit:11];
    }
    [_userName setText:userNameStr];
    [_userName setTextColor:[UIColor whiteColor]];
    [self.contentView addSubview:_userName];
    
    UIImageView *phoneIcon = [[UIImageView alloc] init];
    UIImage *img = [[UIImage imageNamed:@"btn_phone_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    phoneIcon.image = img;
    phoneIcon.tintColor = [UIColor whiteColor];
    phoneIcon.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:phoneIcon];
    
    _phoneNumber = [[UILabel alloc] init];
    [_phoneNumber setText:[[[NSUserDefaults standardUserDefaults] objectForKey:@"userMobile"] stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]];
    [_phoneNumber setTextColor:[UIColor whiteColor]];
    _phoneNumber.font = [UIFont systemFontOfSize:12];
    _phoneNumber.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_phoneNumber];
    
    UIImageView *arrow = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"img_arrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
    arrow.tintColor = [UIColor whiteColor];
    [self.contentView addSubview:arrow];
    
    [_userLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).offset(10);
        make.width.offset(50 * KWidth_Scale);
        make.height.offset(50 * KWidth_Scale);
    }];
    
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userLogo.mas_top).offset(5);
        make.left.equalTo(_userLogo.mas_right).offset(30);
        make.right.equalTo(self.contentView).offset(10);
        make.height.offset(15);
    }];
    
    [phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userName.mas_bottom).offset(15);
        make.left.equalTo(_userLogo.mas_right).offset(30);
        make.width.offset(10);
        make.height.offset(10);
    }];
    
    [_phoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userName.mas_bottom).offset(15);
        make.left.equalTo(phoneIcon.mas_right).offset(2);
        make.right.equalTo(self.contentView).offset(10);
        make.height.offset(10);
    }];
    
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_userLogo);
        make.right.equalTo(self.contentView).offset(-10);
        make.width.offset(15);
        make.height.offset(15);
    }];
}

- (NSString *)createRandomStrwithDigit:(NSInteger)digit
{
    NSString *string = [[NSString alloc]init];
    for (int i = 0; i < digit; i++) {
        int number = arc4random() % 36;
        if (number < 10) {
            int figure = arc4random() % 10;
            NSString *tempString = [NSString stringWithFormat:@"%d", figure];
            string = [string stringByAppendingString:tempString];
        }else {
            int figure = (arc4random() % 26) + 97;
            char character = figure;
            NSString *tempString = [NSString stringWithFormat:@"%c", character];
            string = [string stringByAppendingString:tempString];
        }
    }
    return string;
}

- (void)setUserPortriatUri:(NSString *)userPortriatUri
{
    [_userLogo sd_setImageWithURL:[NSURL URLWithString:userPortriatUri] placeholderImage:[UIImage imageNamed:@"publicNumber"]];
}

- (void)setUserNameText:(NSString *)userNameText
{
    _userNameText = userNameText;
    _userName.text = _userNameText;
}

- (void)setPhoneNumberText:(NSString *)phoneNumberText
{
    _phoneNumberText = phoneNumberText;
    _phoneNumber.text = _phoneNumberText;
}

@end
